<?php
namespace app1\index\model;

use think\Model;

class Post extends Model
{

    
    /** 
     * 发帖函数
     * @param string $title
     * @param string $content
     * @return number[]|string[]
     */
    public function post($title,$content,$user)
    {
        $user_id = $user['id'];
        if($title == null){
            return ["error"=>1,"info"=>"标题不能为空"];
        }
        
        if($content == null){
            return ["error"=>1,"info"=>"内容不能为空"];
        }
        
        $create_time = time();
        $data = [
            'title' => $title,
            'content' => $content,
            'create_time' => time(),
            'user_id' => $user_id
        ];
        
        $res = $this->save($data);
        $post = $this->where('create_time',$create_time)->find();
        if($res){
            return ["post"=>$post,"error"=>0,"info"=>"创建帖子成功"];
        }
        
        return ["error"=>1,"info"=>"创建帖子失败"];
    }
     
    
    /**
     * 帖子详情函数
     * @param int $id
     * @param int $page
     * @return \think\response\Json|number[]|string[]|unknown[]
     */
    public function getPageList($id)
    {
        $res = $this->where('id',$id)->find();
        if($res == null){
            return ["error"=>1,"info"=>"帖子ID不存在"];
        } 
        
        $postDetail = $this->where('id', $id)
        ->whereOr('pid', $id)
        ->paginate(3, false, [
        'query' => $_REQUEST
        ]);
        
        return $postDetail;
    }

   
   /**
    * 帖子删除函数
    * @param int $id
    * @param string $user
    * @return number[]|string[]
    */
    public function postDelete($id,$user)
    {   
        if($id == null){
            return ["error"=>1,"info"=>"帖子ID不能为空"];
        }
        
        if($user['id'] == null){
            return ["error"=>1,"info"=>"离线状态，请先登录再删帖"];
        }
        
        $res = $this->where('id',$id)->find();
        if($res['pid'] == 0){
            $this->where('id', $id)->delete();
            return ['error'=>2,'info'=>'主贴删除成功'];
        }
        
        if($user['id'] != $res['user_id']){
            return ["error"=>1,"info"=>"你没有删帖权限"];
        }
        
        $res = $this->where('id', $id)->delete();
        if($res) {
            return ["error"=>0,"info"=>"贴子删除成功"];
        }
        
        return ["error"=>1,"info"=>"贴子删除失败"];
    }

   
    /** 
     *帖子列表函数
     * @param unknown $page
     * @return number[]|string[]|unknown[]
     */
    public function postList()
    {
        $postList = $this->where('pid', 0)->paginate(3,false,['query'=>$_REQUEST
        ]);    
        return $postList;
    }
    

    /**
     * 帖子编辑函数
     * @param int $id            
     * @param string $title            
     * @param string $content            
     * @param int $user_id            
     * @param date $create_time            
     */
    public function postEdit($id,$content,$title,$user)
    {
        if($user['id'] == null){
            return ["error"=>1,"info"=>"离线状态，请先登录再修改帖子"];
        }
        
        $res = $this->where('id',$id)->find();
        if($user['id'] != $res['user_id']){
            return ["error"=>1,"info"=>"你没有修改帖子的权限"];
        }
        
        if($title == null){
            return ["error"=>1,"info"=>"帖子的标题不能为空"];
        }
        
        if($content == null){
            return ["error"=>1,"info"=>"帖子的内容不能为空"];
        }
        
        $data = [
            'create_time' => time(),
            'content' => $content,
            'title' => $title
        ];
        
        $res = $this->where('id',$id)->update($data);
        $postEdit = $this->where($data)->find();
        if($res) {
            return ["postEdit"=>$postEdit,"error"=>0,"info"=>"帖子编辑成功"];
        }
        
        return ["error"=>1, "info"=>"帖子编辑失败"];
    }

    
    /**
     * 帖子回复函数
     * @param int $id
     * @param string $content
     * @param int $user
     * @return number[]|string[]
     */
    public function postReply($id,$content,$user)
    {
        if($user['id'] == null){
            return ["error"=>1,"info"=>"离线状态，请先登录再回帖"];
        }
        
        $res = $this->where('id',$id)->find();
        if($res == null){
            return ["error"=>1,"info"=>"你要回复的主贴的ID不存在"];  
        }
        
        if($content == null){
            return ["error"=>1,"info"=>"回复的内容不能为空"];
        }
        
        $user_name  = $user['user_name'];
        $user_id    = $user['id'];
        $title      = "{$user_name}回复说:";
        $data = [
            'title'         => $title,
            'pid'           => $id,
            'user_id'       => $user_id,
            'content'       => $content,
            'create_time'   => time()
        ];
        
        $res = $this->save($data);
        $postReply = $this->where($data)->find();
        if($res) {
            return ["postReply"=>$postReply,"error"=>0, "info"=>'回复成功'];
        }
        
        return ["error"=>1, "info"=>'回复失败'];
    }
}
